package com.gwnet.common.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gwnet.common.bean.SysConfig;
import com.gwnet.common.mapper.SysConfigMapper;
import com.gwnet.common.service.SysConfigService;
import com.gwnet.common.util.Json;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author: WangLiang
 * @date: 2024/7/20 10:10
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    private final SysConfigMapper sysConfigMapper;

    @Override
    @Cacheable(cacheNames = "SysConfig", key = "#key")
    public String getValue(String key) {
        SysConfig config = sysConfigMapper.queryByKey(key);
        return config == null ? null : config.getConfigValue();
    }

    @Override
    @Cacheable(cacheNames="SysConfigObject",key="#key")
    public <T> T  getSysConfigObject(String key, Class<T> clazz) {
        String value = getValue(key);
        if (StrUtil.isBlank(value)) {
            return null;
        }
        if(Objects.equals("java.lang.String",clazz.getName())){
            return (T)value;
        }else{
            return Json.parseObject(value, clazz);
        }
    }

}
